package cn.zlp.pet.service.impl;

import cn.zlp.basic.exception.BusinessException;
import cn.zlp.basic.service.impl.BaseServiceImpl;
import cn.zlp.basic.util.LoginContextUtil;
import cn.zlp.org.domain.Employee;
import cn.zlp.org.mapper.EmployeeMapper;
import cn.zlp.pet.domain.Pet;
import cn.zlp.pet.domain.PetDetail;
import cn.zlp.pet.mapper.PetDetailMapper;
import cn.zlp.pet.mapper.PetMapper;
import cn.zlp.pet.service.IPetService;
import cn.zlp.user.domain.Logininfo;
import cn.zlp.user.domain.User;
import cn.zlp.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired(required = false)
    private PetMapper petMapper;

    @Autowired(required = false)
    private PetDetailMapper petDetailMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    /**
     * 添加宠物
     * @param pet
     * @param request
     */
    @Override
    public void add(Pet pet,HttpServletRequest request) {
        // 根据请求获取上下文,然后通过工具类湖区登录信息
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        // 获取登录用户,因为宠物业务是店铺管理员才可以进行新增和修改,所以可以获取店铺信息
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        // 将宠物店铺信息设置上去
        pet.setShop_id(employee.getShop_id());
        //添加宠物信息
        super.add(pet);
        //添加明细
        PetDetail detail = pet.getDetail();
        if(detail != null){ // 判断前端是否填写了明细
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    /**
     * 删除宠物信息
     * @param id
     */
    @Override
    public void del(Long id) {
        super.del(id);
        // 根据宠物id删除宠物明细
        petDetailMapper.removeByPetId(id);
    }

    /**
     * 批量删除宠物信息
     * @param ids
     */
    @Override
    public void batchRemove(List<Long> ids) {
        // 批量删除宠物信息
        super.batchRemove(ids);
        // 根据宠物id批量删除宠物明细
        petDetailMapper.batchRemoveByPetId(ids);
    }

    /**
     * 修该宠物信息
     * @param pet
     */
    @Override
    public void update(Pet pet) {
        // 修改宠物信息
        super.update(pet);
        // 修改明细
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.updateByPetId(detail);
        }
    }

    /**
     * 上线
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        // 将上传id集合放到map集合中
        map.put("ids",ids);
        map.put("onsaletime",new Date());
        petMapper.onSale(map);
    }

    /**
     * 下线
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offsaletime",new Date());
        petMapper.offSale(map);
    }

    /**
     * 宠物领养
     * @param id
     * @param request
     */
    @Override
    public void adopt(Long id, HttpServletRequest request) {
        Pet pet = petMapper.loadById(id);
        // 修改状态,下架
        pet.setState(0);
        // 设置下架时间
        pet.setOffsaletime(new Date());
        // 设置领养用户
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        pet.setUser(user);
        petMapper.update(pet);
        // 订单 @todo
        // 支付 @todo
    }

    /**
     * 判断是否为店铺管理员
     * @param request
     */
    @Override
    public void isShopAdmin(HttpServletRequest request) {
        // 通过工具类获取登录对象
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        // 根据登录信息获取登录用户
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        if (employee.getShop_id() == null){ // 如果登录用户的店铺id为空,说明是平台管理员
            throw new BusinessException("平台管理员");
        }
    }
}
